package com.xm.linxiaogang.utils

import android.app.Activity
import android.app.Application
import android.app.Application.ActivityLifecycleCallbacks
import android.content.ComponentCallbacks
import android.content.res.Configuration
import android.os.Bundle
import android.util.DisplayMetrics

/**
 * 屏幕适配工具
 * 算法思想：监听所有Activity的生命周期，然后根据UI设计图，
 * 动态改变屏幕密度，使得在任何分辨率的设备下，所有的控件都和
 * 设计图保持一样的比例，不会被拉伸变形，以达到适配的目的
 */
object DensityUtils {
    //屏幕密度
    private var appDensity = 0f

    //字体缩放密度
    private var appScaledDensity = 0f

    //屏幕信息
    private var appDisplayMetrics: DisplayMetrics? = null

    //设计图宽度
    private var WIDTH = 0f

    /**
     * 设置屏幕密度
     *
     * @param application 上下文
     * @param width       设计图针中的屏幕宽度
     */
    fun setDensity(application: Application, width: Float) {
        appDisplayMetrics = application.resources.displayMetrics
        WIDTH = width

        if (appDensity == 0f) {
            appDensity = appDisplayMetrics!!.density
        }

        if (appScaledDensity == 0f) {
            appScaledDensity = appDisplayMetrics!!.scaledDensity
        }

        //监听Activity的生命周期变化
        registerActivityLifecycleCallbacks(application)

        //监听系统配置变化
        application.registerComponentCallbacks(object : ComponentCallbacks {
            override fun onConfigurationChanged(newConfig: Configuration) {
                //字体改变后,将appScaledDensity重新赋值
                if (newConfig != null && newConfig.fontScale > 0) {
                    appScaledDensity = application.resources.displayMetrics.scaledDensity
                }
            }

            override fun onLowMemory() {}
        })
    }

    /**
     * 重置密度参数
     *
     * @param activity 上下文
     */
    private fun resetDesity(activity: Activity) {
        //计算屏幕密度比（屏幕宽度像素/设计图宽度）
        val targetDensity = appDisplayMetrics!!.widthPixels / WIDTH
        //计算屏幕密度
        val targetDensityDpi = (160 * targetDensity).toInt()
        //计算字体缩放密度
        val targetScaledDensity = targetDensity * (appScaledDensity / appDensity)
        //最后将修改过后的值赋给系统参数
        val activityDisplayMetrics = activity.resources.displayMetrics
        activityDisplayMetrics.density = targetDensity
        activityDisplayMetrics.densityDpi = targetDensityDpi
        activityDisplayMetrics.scaledDensity = targetScaledDensity
    }

    private fun registerActivityLifecycleCallbacks(application: Application) {

        application.registerActivityLifecycleCallbacks(object : ActivityLifecycleCallbacks {

            override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) {
                resetDesity(activity)
            }

            override fun onActivityStarted(activity: Activity) {

            }

            override fun onActivityResumed(activity: Activity) {

            }

            override fun onActivityPaused(activity: Activity) {

            }

            override fun onActivityStopped(activity: Activity) {

            }

            override fun onActivitySaveInstanceState(activity: Activity, outState: Bundle) {

            }

            override fun onActivityDestroyed(activity: Activity) {

            }
        })
    }
}